package info.rodinia.tokenmaker;

import info.rodinia.tokenmaker.TokenMaker.Attribute;
import info.rodinia.tokenmaker.TokenMaker.Skill;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.Collection;
import java.util.Iterator;
import java.util.TreeMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.JLabel;

import org.apache.commons.lang.StringEscapeUtils;

/**
 * VTToken models a PC DDi VT token. It subclasses Token, which does most of the
 * work. VTtoken specialises by providing overrides to the methods that write
 * out power macros.
 * 
 * @author Blakey, Summer 2010
 * 
 */
public class VTToken extends Token {

    private PC myPC;

    public VTToken(PC newPC) {
	super(newPC);
	setTokenType("PC");
	setName(me.getName());
	myPC = newPC;
    }

    /**
     * Save this token off to disk. This method knows all about the internal
     * format of a DDi VT token and it replicates it and saves the resulting
     * file to disk.
     */

    public void save() {
	if (tokenName == null || tokenFile == null)
	    return;
	makeTokenDir();
	createObject();
	makeZip();
	deleteDir(new File(tokenName));
    }

    private void createObject() {

	try {

	    // Get the PC's attributes
	    int str = me.getAttributes().get(Attribute.Strength);
	    int strmod = (str - 10) / 2;
	    int dex = me.getAttributes().get(Attribute.Dexterity);
	    int dexmod = (dex - 10) / 2;
	    int con = me.getAttributes().get(Attribute.Constitution);
	    int conmod = (con - 10) / 2;
	    int intel = me.getAttributes().get(Attribute.Intelligence);
	    int intmod = (intel - 10) / 2;
	    int wis = me.getAttributes().get(Attribute.Wisdom);
	    int wismod = (wis - 10) / 2;
	    int cha = me.getAttributes().get(Attribute.Charisma);
	    int chamod = (cha - 10) / 2;

	    File file = new File(tokenName + "/object");
	    FileWriter w = new FileWriter(file);
	    BufferedWriter writer = new BufferedWriter(w);
	    writer.write("<com.wizards.games.dnd.common.elements.characters.CharacterSheetImpl>\n");
	    writer.write("<elementID>\n");
	    writer.write("<unique>-1890483758</unique>\n");
	    writer.write("<time>1294335863106</time>\n");
	    writer.write("<count>-32762</count>\n");
	    writer.write("</elementID>\n");
	    writer.write("<owner></owner>\n");
	    writer.write("<name>" + me.getName() + "</name>\n");
	    writer.write("<description>VT token generated by PCMaker. Written by Blakey 2010/11</description>\n");
	    writer.write("<tokenProperty>GenericMedium</tokenProperty>\n");
	    writer.write("<currentHP>" + me.getHP() + "</currentHP>\n");
	    writer.write("<maxHP>" + me.getHP() + "</maxHP>\n");
	    writer.write("<tempHP>0</tempHP>\n");
	    writer.write("<invisible>false</invisible>\n");
	    writer.write("<properties/>\n");
	    writer.write("<bloody>false</bloody>\n");
	    writer.write("<race>" + myPC.getRace() + "</race>\n");
	    writer.write("<charClass>" + myPC.getPCClass() + "</charClass>\n");
	    writer.write("<level>" + me.getLevel() + "</level>\n");
	    writer.write("<stats>\n");
	    writer.write("<entry>\n");
	    writer.write("<com.wizards.games.dnd.common.elements.characters.CharacterSheetStats>DEXTERITY</com.wizards.games.dnd.common.elements.characters.CharacterSheetStats>\n");
	    writer.write("<int>" + dex + "</int>\n");
	    writer.write("</entry>\n");
	    writer.write("<entry>\n");
	    writer.write("<com.wizards.games.dnd.common.elements.characters.CharacterSheetStats>INTELLIGENCE_BONUS</com.wizards.games.dnd.common.elements.characters.CharacterSheetStats>\n");
	    writer.write("<int>" + intmod + "</int>\n");
	    writer.write("</entry>\n");
	    writer.write("<entry>\n");
	    writer.write("<com.wizards.games.dnd.common.elements.characters.CharacterSheetStats>FAILED_SAVES</com.wizards.games.dnd.common.elements.characters.CharacterSheetStats>\n");
	    writer.write("<int>0</int>\n");
	    writer.write("</entry>\n");
	    writer.write("<entry>\n");
	    writer.write("<com.wizards.games.dnd.common.elements.characters.CharacterSheetStats>STRENGTH_BONUS</com.wizards.games.dnd.common.elements.characters.CharacterSheetStats>\n");
	    writer.write("<int>" + strmod + "</int>\n");
	    writer.write("</entry>\n");
	    writer.write("<entry>\n");
	    writer.write("<com.wizards.games.dnd.common.elements.characters.CharacterSheetStats>SURGE_VALUE</com.wizards.games.dnd.common.elements.characters.CharacterSheetStats>\n");
	    writer.write("<int>" + me.getHealingSurgeValue() + "</int>\n");
	    writer.write("</entry>\n");
	    writer.write("<entry>\n");
	    writer.write("<com.wizards.games.dnd.common.elements.characters.CharacterSheetStats>PASSIVE_INSIGHT</com.wizards.games.dnd.common.elements.characters.CharacterSheetStats>\n");
	    writer.write("<int>" + (10 + me.getSkills().get(Skill.Insight))
		    + "</int>\n");
	    writer.write("</entry>\n");
	    writer.write("<entry>\n");
	    writer.write("<com.wizards.games.dnd.common.elements.characters.CharacterSheetStats>CONSTITUTION_BONUS</com.wizards.games.dnd.common.elements.characters.CharacterSheetStats>\n");
	    writer.write("<int>" + conmod + "</int>\n");
	    writer.write("</entry>\n");
	    writer.write("<entry>\n");
	    writer.write("<com.wizards.games.dnd.common.elements.characters.CharacterSheetStats>REFLEX</com.wizards.games.dnd.common.elements.characters.CharacterSheetStats>\n");
	    writer.write("<int>" + me.getReflex() + "</int>\n");
	    writer.write("</entry>\n");
	    writer.write("<entry>\n");
	    writer.write("<com.wizards.games.dnd.common.elements.characters.CharacterSheetStats>WISDOM_BONUS</com.wizards.games.dnd.common.elements.characters.CharacterSheetStats>\n");
	    writer.write("<int>" + wismod + "</int>\n");
	    writer.write("</entry>\n");
	    writer.write("<entry>\n");
	    writer.write("<com.wizards.games.dnd.common.elements.characters.CharacterSheetStats>CHARISMA_BONUS</com.wizards.games.dnd.common.elements.characters.CharacterSheetStats>\n");
	    writer.write("<int>" + chamod + "</int>\n");
	    writer.write("</entry>\n");
	    writer.write("<entry>\n");
	    writer.write("<com.wizards.games.dnd.common.elements.characters.CharacterSheetStats>STRENGTH</com.wizards.games.dnd.common.elements.characters.CharacterSheetStats>\n");
	    writer.write("<int>" + str + "</int>\n");
	    writer.write("</entry>\n");
	    writer.write("<entry>\n");
	    writer.write("<com.wizards.games.dnd.common.elements.characters.CharacterSheetStats>FORTITUDE</com.wizards.games.dnd.common.elements.characters.CharacterSheetStats>\n");
	    writer.write("<int>" + me.getFortitude() + "</int>\n");
	    writer.write("</entry>\n");
	    writer.write("<entry>\n");
	    writer.write("<com.wizards.games.dnd.common.elements.characters.CharacterSheetStats>MILESTONES</com.wizards.games.dnd.common.elements.characters.CharacterSheetStats>\n");
	    writer.write("<int>0</int>\n");
	    writer.write("</entry>\n");
	    writer.write("<entry>\n");
	    writer.write("<com.wizards.games.dnd.common.elements.characters.CharacterSheetStats>ARMOR_CLASS</com.wizards.games.dnd.common.elements.characters.CharacterSheetStats>\n");
	    writer.write("<int>" + me.getAC() + "</int>\n");
	    writer.write("</entry>\n");
	    writer.write("<entry>\n");
	    writer.write("<com.wizards.games.dnd.common.elements.characters.CharacterSheetStats>WISDOM</com.wizards.games.dnd.common.elements.characters.CharacterSheetStats>\n");
	    writer.write("<int>" + wis + "</int>\n");
	    writer.write("</entry>\n");
	    writer.write("<entry>\n");
	    writer.write("<com.wizards.games.dnd.common.elements.characters.CharacterSheetStats>DEXTERITY_BONUS</com.wizards.games.dnd.common.elements.characters.CharacterSheetStats>\n");
	    writer.write("<int>" + dexmod + "</int>\n");
	    writer.write("</entry>\n");
	    writer.write("<entry>\n");
	    writer.write("<com.wizards.games.dnd.common.elements.characters.CharacterSheetStats>CHARISMA</com.wizards.games.dnd.common.elements.characters.CharacterSheetStats>\n");
	    writer.write("<int>" + cha + "</int>\n");
	    writer.write("</entry>\n");
	    writer.write("<entry>\n");
	    writer.write("<com.wizards.games.dnd.common.elements.characters.CharacterSheetStats>PASSIVE_PERCEPTION</com.wizards.games.dnd.common.elements.characters.CharacterSheetStats>\n");
	    writer.write("<int>" + (10 + me.getSkills().get(Skill.Perception))
		    + "</int>\n");
	    writer.write("</entry>\n");
	    writer.write("<entry>\n");
	    writer.write("<com.wizards.games.dnd.common.elements.characters.CharacterSheetStats>WILLPOWER</com.wizards.games.dnd.common.elements.characters.CharacterSheetStats>\n");
	    writer.write("<int>" + me.getWill() + "</int>\n");
	    writer.write("</entry>\n");
	    writer.write("<entry>\n");
	    writer.write("<com.wizards.games.dnd.common.elements.characters.CharacterSheetStats>SURGES_PER_DAY</com.wizards.games.dnd.common.elements.characters.CharacterSheetStats>\n");
	    writer.write("<int>" + me.getHealingSurges() + "</int>\n");
	    writer.write("</entry>\n");
	    writer.write("<entry>\n");
	    writer.write("<com.wizards.games.dnd.common.elements.characters.CharacterSheetStats>SURGES_REMAINING</com.wizards.games.dnd.common.elements.characters.CharacterSheetStats>\n");
	    writer.write("<int>" + me.getHealingSurges() + "</int>\n");
	    writer.write("</entry>\n");
	    writer.write("<entry>\n");
	    writer.write("<com.wizards.games.dnd.common.elements.characters.CharacterSheetStats>SPEED</com.wizards.games.dnd.common.elements.characters.CharacterSheetStats>\n");
	    writer.write("<int>" + me.getSpeed() + "</int>\n");
	    writer.write("</entry>\n");
	    writer.write("<entry>\n");
	    writer.write("<com.wizards.games.dnd.common.elements.characters.CharacterSheetStats>ACTION_POINTS</com.wizards.games.dnd.common.elements.characters.CharacterSheetStats>\n");
	    writer.write("<int>" + me.getActionPoints() + "</int>\n");
	    writer.write("</entry>\n");
	    writer.write("<entry>\n");
	    writer.write("<com.wizards.games.dnd.common.elements.characters.CharacterSheetStats>CONSTITUTION</com.wizards.games.dnd.common.elements.characters.CharacterSheetStats>\n");
	    writer.write("<int>" + con + "</int>\n");
	    writer.write("</entry>\n");
	    writer.write("<entry>\n");
	    writer.write("<com.wizards.games.dnd.common.elements.characters.CharacterSheetStats>INITIATIVE</com.wizards.games.dnd.common.elements.characters.CharacterSheetStats>\n");
	    writer.write("<int>" + me.getInitiative() + "</int>\n");
	    writer.write("</entry>\n");
	    writer.write("<entry>\n");
	    writer.write("<com.wizards.games.dnd.common.elements.characters.CharacterSheetStats>INTELLIGENCE</com.wizards.games.dnd.common.elements.characters.CharacterSheetStats>\n");
	    writer.write("<int>" + intel + "</int>\n");
	    writer.write("</entry>\n");
	    writer.write("</stats>\n");

	    // Now write out the powers...

	    writer.write("<powers>\n");

	    int count = 0;
	    for (Power p : me.getPowers()) {
		writer.write("<com.wizards.games.dnd.common.elements.characters.PlayerPower>\n");
		writer.write("<elementID>\n");
		writer.write("<unique>-1890483758</unique>\n");
		writer.write("<time>1294335863106</time>\n");
		writer.write("<count>" + ++count + "</count>\n");
		writer.write("</elementID>\n");
		writer.write("<name>" + p.getName() + "</name>\n");
		writer.write("<description>"+p.getActionType()+"</description>\n");
		
		writer.write("<rolls>\n");
		for (PowerWeapon pw : p.getWeapons()) {
		    	if (pw.getName().equals("Unarmed")) continue;
		    	
		    	// Attack First
		    	writer.write("<com.wizards.games.dnd.common.elements.characters.SavedRoll>\n");
			writer.write("<elementID>\n");
			writer.write("<unique>-1890483758</unique>\n");
			writer.write("<time>1294335863106</time>\n");
			writer.write("<count>" + ++count + "</count>\n");
			writer.write("</elementID>\n");
			writer.write("<name>Attack: " + p.getName() + " (" + pw.getName()+") vs "+ pw.getDefence()+"</name>\n");
			writer.write("<dice>1d20+"+pw.getAttackBonus()+"</dice>\n");
			writer.write("</com.wizards.games.dnd.common.elements.characters.SavedRoll>\n");
			
			// Now Damage
			writer.write("<com.wizards.games.dnd.common.elements.characters.SavedRoll>\n");
			writer.write("<elementID>\n");
			writer.write("<unique>-1890483758</unique>\n");
			writer.write("<time>1294335863106</time>\n");
			writer.write("<count>" + ++count + "</count>\n");
			writer.write("</elementID>\n");
			writer.write("<name>Damage: " + p.getName() + " (" + pw.getName()+")</name>\n");
			writer.write("<dice>"+pw.getDamageString()+"</dice>\n");
			writer.write("</com.wizards.games.dnd.common.elements.characters.SavedRoll>\n");
		}
		writer.write("</rolls>\n");
		writer.write("<memSize>3379</memSize>\n");

		if (p.getUsage().toLowerCase().equals("at-will")) {
			writer.write("<type>AT_WILL</type>\n");
		}
		else if (p.getUsage().toLowerCase().equals("encounter")) {
			writer.write("<type>ENCOUNTER</type>\n");
		} 
		else if (p.getUsage().toLowerCase().equals("daily")) {
			writer.write("<type>DAILY</type>\n");
		} 
		writer.write("<source>OTHER</source>\n");
		writer.write("<used>false</used>\n");
		writer.write("</com.wizards.games.dnd.common.elements.characters.PlayerPower>\n");
	    }

	    writer.write("</powers>\n");

	    /***
	     * writer.write(
	     * "<com.wizards.games.dnd.common.elements.characters.PlayerPower>\n"
	     * ); writer.write("<elementID>\n");
	     * writer.write("<unique>-1890483758</unique>\n");
	     * writer.write("<time>1294335863106</time>\n");
	     * writer.write("<count>1</count>\n");
	     * writer.write("</elementID>\n");
	     * writer.write("<name>Acrobatic Strike</name>\n");
	     * writer.write("<description>Martial, Weapon&#xd;\n");
	     * writer.write("Standard Action      Melee weapon&#xd;\n");
	     * writer.write
	     * ("Prerequisite: You must be trained in Acrobatics.&#xd;\n");
	     * writer
	     * .write("Requirement: You must be wielding a light blade.&#xd;\n"
	     * ); writer.write("Target: One creature&#xd;\n");
	     * writer.write("Attack: Dexterity vs. AC&#xd;\n"); writer.write(
	     * "Hit: 1[W] + Dexterity modifier damage. If you are grabbed, you escape the grab.&#xd;\n"
	     * ); writer.write(
	     * "Effect: Before or after the attack, you shift 1 square.</description>\n"
	     * ); writer.write("<rolls>\n"); writer.write(
	     * "<com.wizards.games.dnd.common.elements.characters.SavedRoll>\n"
	     * ); writer.write("<elementID>\n");
	     * writer.write("<unique>-1890483758</unique>\n");
	     * writer.write("<time>1294335863106</time>\n");
	     * writer.write("<count>2</count>\n");
	     * writer.write("</elementID>\n");
	     * writer.write("<name>Acrobatic Strike vs AC</name>\n");
	     * writer.write("<dice>1d20 + 9</dice>\n"); writer.write(
	     * "</com.wizards.games.dnd.common.elements.characters.SavedRoll>\n"
	     * ); writer.write(
	     * "<com.wizards.games.dnd.common.elements.characters.SavedRoll>\n"
	     * ); writer.write("<elementID>\n");
	     * writer.write("<unique>-1890483758</unique>\n");
	     * writer.write("<time>1294335863106</time>\n");
	     * writer.write("<count>3</count>\n");
	     * writer.write("</elementID>\n");
	     * writer.write("<name>Acrobatic Strike Damage</name>\n");
	     * writer.write("<dice>1d4+5</dice>\n"); writer.write(
	     * "</com.wizards.games.dnd.common.elements.characters.SavedRoll>\n"
	     * ); writer.write(
	     * "<com.wizards.games.dnd.common.elements.characters.SavedRoll>\n"
	     * ); writer.write("<elementID>\n");
	     * writer.write("<unique>-1890483758</unique>\n");
	     * writer.write("<time>1294335863106</time>\n");
	     * writer.write("<count>4</count>\n");
	     * writer.write("</elementID>\n");
	     * writer.write("<name>Acrobatic Strike Damage (inc SA)</name>\n");
	     * writer.write("<dice>1d4+2d8+7</dice>\n"); writer.write(
	     * "</com.wizards.games.dnd.common.elements.characters.SavedRoll>\n"
	     * ); writer.write("</rolls>\n");
	     * writer.write("<memSize>3379</memSize>\n");
	     * writer.write("<type>AT_WILL</type>\n");
	     * writer.write("<source>OTHER</source>\n");
	     * writer.write("<used>false</used>\n"); writer.write(
	     * "</com.wizards.games.dnd.common.elements.characters.PlayerPower>\n"
	     * );
	     * 
	     * writer.write(
	     * "<com.wizards.games.dnd.common.elements.characters.PlayerPower>\n"
	     * ); writer.write("<elementID>\n");
	     * writer.write("<unique>-1890483758</unique>\n");
	     * writer.write("<time>1294335863106</time>\n");
	     * writer.write("<count>5</count>\n");
	     * writer.write("</elementID>\n");
	     * writer.write("<name>My Power</name>\n"); writer.write(
	     * "<description>Nothing to see here.</description>\n");
	     * writer.write("<rolls>\n"); writer.write(
	     * "<com.wizards.games.dnd.common.elements.characters.SavedRoll>\n"
	     * ); writer.write("<elementID>\n");
	     * writer.write("<unique>-1890483758</unique>\n");
	     * writer.write("<time>1294335863106</time>\n");
	     * writer.write("<count>6</count>\n");
	     * writer.write("</elementID>\n");
	     * writer.write("<name>Attack</name>\n");
	     * writer.write("<dice>1d20+9</dice>\n"); writer.write(
	     * "</com.wizards.games.dnd.common.elements.characters.SavedRoll>\n"
	     * ); writer.write(
	     * "<com.wizards.games.dnd.common.elements.characters.SavedRoll>\n"
	     * ); writer.write("<elementID>\n");
	     * writer.write("<unique>-1890483758</unique>\n");
	     * writer.write("<time>1294335863106</time>\n");
	     * writer.write("<count>7</count>\n");
	     * writer.write("</elementID>\n");
	     * writer.write("<name>Damage</name>\n");
	     * writer.write("<dice>1d4+5</dice>\n"); writer.write(
	     * "</com.wizards.games.dnd.common.elements.characters.SavedRoll>\n"
	     * ); writer.write("</rolls>\n");
	     * writer.write("<memSize>3379</memSize>\n");
	     * writer.write("<type>AT_WILL</type>\n");
	     * writer.write("<source>OTHER</source>\n");
	     * writer.write("<used>false</used>\n"); writer.write(
	     * "</com.wizards.games.dnd.common.elements.characters.PlayerPower>\n"
	     * );
	     * 
	     * writer.write(
	     * "<com.wizards.games.dnd.common.elements.characters.PlayerPower>\n"
	     * ); writer.write("<elementID>\n");
	     * writer.write("<unique>-1890483758</unique>\n");
	     * writer.write("<time>1294335863106</time>\n");
	     * writer.write("<count>8</count>\n");
	     * writer.write("</elementID>\n");
	     * writer.write("<name>My Other Power</name>\n"); writer.write(
	     * "<description>Nothing to see here.</description>\n");
	     * writer.write("<rolls>\n"); writer.write(
	     * "<com.wizards.games.dnd.common.elements.characters.SavedRoll>\n"
	     * ); writer.write("<elementID>\n");
	     * writer.write("<unique>-1890483758</unique>\n");
	     * writer.write("<time>1294335863106</time>\n");
	     * writer.write("<count>9</count>\n");
	     * writer.write("</elementID>\n");
	     * writer.write("<name>Attack</name>\n");
	     * writer.write("<dice>1d20+7</dice>\n"); writer.write(
	     * "</com.wizards.games.dnd.common.elements.characters.SavedRoll>\n"
	     * ); writer.write(
	     * "<com.wizards.games.dnd.common.elements.characters.SavedRoll>\n"
	     * ); writer.write("<elementID>\n");
	     * writer.write("<unique>-1890483758</unique>\n");
	     * writer.write("<time>1294335863106</time>\n");
	     * writer.write("<count>10</count>\n");
	     * writer.write("</elementID>\n");
	     * writer.write("<name>Damage</name>\n");
	     * writer.write("<dice>1d4+1</dice>\n"); writer.write(
	     * "</com.wizards.games.dnd.common.elements.characters.SavedRoll>\n"
	     * ); writer.write("</rolls>\n");
	     * writer.write("<memSize>3379</memSize>\n");
	     * writer.write("<type>ENCOUNTER</type>\n");
	     * writer.write("<source>OTHER</source>\n");
	     * writer.write("<used>false</used>\n"); writer.write(
	     * "</com.wizards.games.dnd.common.elements.characters.PlayerPower>\n"
	     * );
	     ***/

	    // Now do skills

	    writer.write("<skills>\n");
	    writer.write("<com.wizards.games.dnd.common.elements.characters.Skill>\n");
	    writer.write("<elementID>\n");
	    writer.write("<unique>224375185</unique>\n");
	    writer.write("<time>1293643727812</time>\n");
	    writer.write("<count>-32760</count>\n");
	    writer.write("</elementID>\n");
	    writer.write("<name>Acrobatics</name>\n");
	    writer.write("<dice>" + me.getSkills().get(Skill.Acrobatics)
		    + "</dice>\n");
	    writer.write("<trained>false</trained>\n");
	    writer.write("</com.wizards.games.dnd.common.elements.characters.Skill>\n");
	    writer.write("<com.wizards.games.dnd.common.elements.characters.Skill>\n");
	    writer.write("<elementID>\n");
	    writer.write("<unique>224375185</unique>\n");
	    writer.write("<time>1293643727812</time>\n");
	    writer.write("<count>-32759</count>\n");
	    writer.write("</elementID>\n");
	    writer.write("<name>Arcana</name>\n");
	    writer.write("<dice>" + me.getSkills().get(Skill.Arcana)
		    + "</dice>\n");
	    writer.write("<trained>false</trained>\n");
	    writer.write("</com.wizards.games.dnd.common.elements.characters.Skill>\n");
	    writer.write("<com.wizards.games.dnd.common.elements.characters.Skill>\n");
	    writer.write("<elementID>\n");
	    writer.write("<unique>224375185</unique>\n");
	    writer.write("<time>1293643727812</time>\n");
	    writer.write("<count>-32758</count>\n");
	    writer.write("</elementID>\n");
	    writer.write("<name>Athletics</name>\n");
	    writer.write("<dice>" + me.getSkills().get(Skill.Athletics)
		    + "</dice>\n");
	    writer.write("<trained>false</trained>\n");
	    writer.write("</com.wizards.games.dnd.common.elements.characters.Skill>\n");
	    writer.write("<com.wizards.games.dnd.common.elements.characters.Skill>\n");
	    writer.write("<elementID>\n");
	    writer.write("<unique>224375185</unique>\n");
	    writer.write("<time>1293643727812</time>\n");
	    writer.write("<count>-32757</count>\n");
	    writer.write("</elementID>\n");
	    writer.write("<name>Bluff</name>\n");
	    writer.write("<dice>" + me.getSkills().get(Skill.Bluff)
		    + "</dice>\n");
	    writer.write("<trained>false</trained>\n");
	    writer.write("</com.wizards.games.dnd.common.elements.characters.Skill>\n");
	    writer.write("<com.wizards.games.dnd.common.elements.characters.Skill>\n");
	    writer.write("<elementID>\n");
	    writer.write("<unique>224375185</unique>\n");
	    writer.write("<time>1293643727812</time>\n");
	    writer.write("<count>-32756</count>\n");
	    writer.write("</elementID>\n");
	    writer.write("<name>Diplomacy</name>\n");
	    writer.write("<dice>" + me.getSkills().get(Skill.Diplomacy)
		    + "</dice>\n");
	    writer.write("<trained>false</trained>\n");
	    writer.write("</com.wizards.games.dnd.common.elements.characters.Skill>\n");
	    writer.write("<com.wizards.games.dnd.common.elements.characters.Skill>\n");
	    writer.write("<elementID>\n");
	    writer.write("<unique>224375185</unique>\n");
	    writer.write("<time>1293643727812</time>\n");
	    writer.write("<count>-32755</count>\n");
	    writer.write("</elementID>\n");
	    writer.write("<name>Dungeoneering</name>\n");
	    writer.write("<dice>" + me.getSkills().get(Skill.Dungeoneering)
		    + "</dice>\n");
	    writer.write("<trained>false</trained>\n");
	    writer.write("</com.wizards.games.dnd.common.elements.characters.Skill>\n");
	    writer.write("<com.wizards.games.dnd.common.elements.characters.Skill>\n");
	    writer.write("<elementID>\n");
	    writer.write("<unique>224375185</unique>\n");
	    writer.write("<time>1293643727812</time>\n");
	    writer.write("<count>-32754</count>\n");
	    writer.write("</elementID>\n");
	    writer.write("<name>Endurance</name>\n");
	    writer.write("<dice>" + me.getSkills().get(Skill.Endurance)
		    + "</dice>\n");
	    writer.write("<trained>false</trained>\n");
	    writer.write("</com.wizards.games.dnd.common.elements.characters.Skill>\n");
	    writer.write("<com.wizards.games.dnd.common.elements.characters.Skill>\n");
	    writer.write("<elementID>\n");
	    writer.write("<unique>224375185</unique>\n");
	    writer.write("<time>1293643727812</time>\n");
	    writer.write("<count>-32753</count>\n");
	    writer.write("</elementID>\n");
	    writer.write("<name>Heal</name>\n");
	    writer.write("<dice>" + me.getSkills().get(Skill.Heal)
		    + "</dice>\n");
	    writer.write("<trained>false</trained>\n");
	    writer.write("</com.wizards.games.dnd.common.elements.characters.Skill>\n");
	    writer.write("<com.wizards.games.dnd.common.elements.characters.Skill>\n");
	    writer.write("<elementID>\n");
	    writer.write("<unique>224375185</unique>\n");
	    writer.write("<time>1293643727812</time>\n");
	    writer.write("<count>-32752</count>\n");
	    writer.write("</elementID>\n");
	    writer.write("<name>History</name>\n");
	    writer.write("<dice>" + me.getSkills().get(Skill.History)
		    + "</dice>\n");
	    writer.write("<trained>false</trained>\n");
	    writer.write("</com.wizards.games.dnd.common.elements.characters.Skill>\n");
	    writer.write("<com.wizards.games.dnd.common.elements.characters.Skill>\n");
	    writer.write("<elementID>\n");
	    writer.write("<unique>224375185</unique>\n");
	    writer.write("<time>1293643727812</time>\n");
	    writer.write("<count>-32751</count>\n");
	    writer.write("</elementID>\n");
	    writer.write("<name>Insight</name>\n");
	    writer.write("<dice>" + me.getSkills().get(Skill.Insight)
		    + "</dice>\n");
	    writer.write("<trained>false</trained>\n");
	    writer.write("</com.wizards.games.dnd.common.elements.characters.Skill>\n");
	    writer.write("<com.wizards.games.dnd.common.elements.characters.Skill>\n");
	    writer.write("<elementID>\n");
	    writer.write("<unique>224375185</unique>\n");
	    writer.write("<time>1293643727812</time>\n");
	    writer.write("<count>-32750</count>\n");
	    writer.write("</elementID>\n");
	    writer.write("<name>Intimidate</name>\n");
	    writer.write("<dice>" + me.getSkills().get(Skill.Intimidate)
		    + "</dice>\n");
	    writer.write("<trained>false</trained>\n");
	    writer.write("</com.wizards.games.dnd.common.elements.characters.Skill>\n");
	    writer.write("<com.wizards.games.dnd.common.elements.characters.Skill>\n");
	    writer.write("<elementID>\n");
	    writer.write("<unique>224375185</unique>\n");
	    writer.write("<time>1293643727812</time>\n");
	    writer.write("<count>-32749</count>\n");
	    writer.write("</elementID>\n");
	    writer.write("<name>Nature</name>\n");
	    writer.write("<dice>" + me.getSkills().get(Skill.Nature)
		    + "</dice>\n");
	    writer.write("<trained>false</trained>\n");
	    writer.write("</com.wizards.games.dnd.common.elements.characters.Skill>\n");
	    writer.write("<com.wizards.games.dnd.common.elements.characters.Skill>\n");
	    writer.write("<elementID>\n");
	    writer.write("<unique>224375185</unique>\n");
	    writer.write("<time>1293643727812</time>\n");
	    writer.write("<count>-32748</count>\n");
	    writer.write("</elementID>\n");
	    writer.write("<name>Perception</name>\n");
	    writer.write("<dice>" + me.getSkills().get(Skill.Perception)
		    + "</dice>\n");
	    writer.write("<trained>false</trained>\n");
	    writer.write("</com.wizards.games.dnd.common.elements.characters.Skill>\n");
	    writer.write("<com.wizards.games.dnd.common.elements.characters.Skill>\n");
	    writer.write("<elementID>\n");
	    writer.write("<unique>224375185</unique>\n");
	    writer.write("<time>1293643727812</time>\n");
	    writer.write("<count>-32747</count>\n");
	    writer.write("</elementID>\n");
	    writer.write("<name>Religion</name>\n");
	    writer.write("<dice>" + me.getSkills().get(Skill.Religion)
		    + "</dice>\n");
	    writer.write("<trained>false</trained>\n");
	    writer.write("</com.wizards.games.dnd.common.elements.characters.Skill>\n");
	    writer.write("<com.wizards.games.dnd.common.elements.characters.Skill>\n");
	    writer.write("<elementID>\n");
	    writer.write("<unique>224375185</unique>\n");
	    writer.write("<time>1293643727812</time>\n");
	    writer.write("<count>-32746</count>\n");
	    writer.write("</elementID>\n");
	    writer.write("<name>Stealth</name>\n");
	    writer.write("<dice>" + me.getSkills().get(Skill.Stealth)
		    + "</dice>\n");
	    writer.write("<trained>false</trained>\n");
	    writer.write("</com.wizards.games.dnd.common.elements.characters.Skill>\n");
	    writer.write("<com.wizards.games.dnd.common.elements.characters.Skill>\n");
	    writer.write("<elementID>\n");
	    writer.write("<unique>224375185</unique>\n");
	    writer.write("<time>1293643727812</time>\n");
	    writer.write("<count>-32745</count>\n");
	    writer.write("</elementID>\n");
	    writer.write("<name>Streetwise</name>\n");
	    writer.write("<dice>" + me.getSkills().get(Skill.Streetwise)
		    + "</dice>\n");
	    writer.write("<trained>false</trained>\n");
	    writer.write("</com.wizards.games.dnd.common.elements.characters.Skill>\n");
	    writer.write("<com.wizards.games.dnd.common.elements.characters.Skill>\n");
	    writer.write("<elementID>\n");
	    writer.write("<unique>224375185</unique>\n");
	    writer.write("<time>1293643727812</time>\n");
	    writer.write("<count>-32744</count>\n");
	    writer.write("</elementID>\n");
	    writer.write("<name>Thievery</name>\n");
	    writer.write("<dice>" + me.getSkills().get(Skill.Thievery)
		    + "</dice>\n");
	    writer.write("<trained>false</trained>\n");
	    writer.write("</com.wizards.games.dnd.common.elements.characters.Skill>\n");
	    writer.write("</skills>\n");
	    writer.write("<otherRolls>\n");
	    writer.write("<com.wizards.games.dnd.common.elements.characters.SavedRoll>\n");
	    writer.write("<elementID>\n");
	    writer.write("<unique>224375185</unique>\n");
	    writer.write("<time>1293643727812</time>\n");
	    writer.write("<count>-31547</count>\n");
	    writer.write("</elementID>\n");
	    writer.write("<name>Saving Throw</name>\n");
	    writer.write("<dice>1d20</dice>\n");
	    writer.write("</com.wizards.games.dnd.common.elements.characters.SavedRoll>\n");
	    writer.write("</otherRolls>\n");
	    writer.write("<secondWindUsed>false</secondWindUsed>\n");
	    writer.write("<memSize>68174</memSize>\n");
	    writer.write("</com.wizards.games.dnd.common.elements.characters.CharacterSheetImpl>\n");

	    writer.close();

	} catch (Exception e) {
	    System.err.println("Error saving contents file for token " + name
		    + ": " + e);
	}
    }
}
